package com.smartrobot.smartrobot.circle;

import android.content.Context;
import android.os.Message;

import android.util.Log;
import com.smartrobot.smartrobot.Common.Common;
import com.smartrobot.smartrobot.Common.CommonFunc;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.pojo.circleDetailPojo;
import com.smartrobot.smartrobot.pojo.getFriendListPojo;

import com.smartrobot.smartrobot.pojo.saveFriendListPojo;
import com.smartrobot.smartrobot.util.User;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2014/12/23.
 */
public class FriendListCallBack {




    //获取好友列表成功的返回
    public void GetFriendListSuccess(Context context,JSONObject response,final Handler handler,final Object object){
        try{

            String sucMsg  =  response.getString("msg");
            String status = response.getString("status");
            LogToSd.debug("打印获取好友列表成功的信息");
            LogToSd.debug(sucMsg);

           if(status.equals(commonData.DATA_NORMAL)){


               List<getFriendListPojo> friendList = new   ArrayList<getFriendListPojo>();
               List<getFriendListPojo> customerList = new   ArrayList<getFriendListPojo>();
               List<getFriendListPojo> barberList = new   ArrayList<getFriendListPojo>();
               JSONObject res = response.getJSONObject("res");

               JSONArray customer = res.optJSONArray("Sof_friends_msg");
               JSONArray friend = res.optJSONArray("My_friends_msg");
               JSONObject barber = res.optJSONObject("Barber_msg");

               if(friend!=null)
               for(int i=0;i<friend.length();i++) {
                   getFriendListPojo friendListPojo = new getFriendListPojo();
                   JSONObject tempJson = friend.getJSONObject(i);
                   friendListPojo.setHeadImg(tempJson.getString("u_head"));
                   friendListPojo.setName(tempJson.getString("u_nickname"));
                   friendListPojo.setSex(tempJson.getString("u_sex"));
                   friendListPojo.setSign(tempJson.getString("u_signature"));
                   friendListPojo.setUserId(tempJson.getString("u_id"));
                   friendListPojo.setUserAccount(tempJson.getString("u_phone"));
                   friendListPojo.setReName(tempJson.getString("u_name"));
                   friendListPojo.setRole(tempJson.getString("u_role"));

                   friendList.add(friendListPojo);

//                   for(int j=0;j<100;j++){
//                       friendList.add(friendListPojo);
//                   }
               }
//

               if(customer!=null)
               for(int i=0;i<customer.length();i++){
                   getFriendListPojo customerListPojo = new getFriendListPojo();
                   JSONObject tempJson = customer.getJSONObject(i);
                   customerListPojo.setHeadImg(tempJson.getString("u_head"));
                   customerListPojo.setName(tempJson.getString("u_nickname"));
                   customerListPojo.setSex(tempJson.getString("u_sex"));
                   customerListPojo.setSign(tempJson.getString("u_signature"));
                   customerListPojo.setUserId(tempJson.getString("u_id"));
                   customerListPojo.setUserAccount(tempJson.getString("u_phone"));
                   customerListPojo.setReName(tempJson.getString("u_name"));
                   customerListPojo.setRole(tempJson.getString("u_role"));
                   customerList.add(customerListPojo);
               }


               String role  =  res.getString("My_role");

               if(role.equals("0")&&barber!=null){//客户
//                   JSONObject barber = res.getJSONObject("Barber_msg");
                   JSONObject tempJson = barber;
                   getFriendListPojo barberListPojo = new getFriendListPojo();
                   barberListPojo.setHeadImg(tempJson.getString("u_head"));
                   barberListPojo.setName(tempJson.getString("u_nickname"));
                   barberListPojo.setSex(tempJson.getString("u_sex"));
                   barberListPojo.setSign(tempJson.getString("u_signature"));
                   barberListPojo.setUserId(tempJson.getString("u_id"));
                   barberListPojo.setUserAccount(tempJson.getString("u_phone"));
                   barberListPojo.setReName(tempJson.getString("u_name"));
                   barberListPojo.setRole(tempJson.getString("u_role"));
                   if( !barberListPojo.getUserId().equals(""))
                        barberList.add(barberListPojo);
               }




               //保存入本地数据库
               if(User.getInstance()==null){
                   User.getUserInfo(context);
               }


               if( !CircleUtil.deleteAllFriendList(context,User.getInstance().getId())){   //清除原来的数据
                   throw new Exception("clear friend data failed");
               }


               saveFriendListPojo circleFriends = new saveFriendListPojo(barberList,friendList,customerList);
               CircleUtil.saveFriendList(circleFriends,context,User.getInstance().getId());  //加入新的好友数据




               String  acName = CommonFunc.getRunningActivityName(context);
               Log.e("当前活动的activity",acName);


               //返回activity
               Map<String,Object> map  = new HashMap<String, Object>();
               map.put("barberList",barberList);
               map.put("friendList",friendList);
               map.put("customerList",customerList);
               map.put("role",role);


               Message message = new Message();
               message.what = commonData.GET_FRIEND_LIST_SUCCESS_CODE;
               message.obj = map;

               if(handler!=null){
                   Log.e("========更新好友成功=====","我要返回来啦");
                   handler.sendMessage(message);
               }else{
                   CircleUtil.isFriendChange = true;
               }


            }else if(status.equals("100")){
               Message message = new Message();
               message.arg1 = commonData.GET_FRIEND_LIST_WITHOUT_LOGIN;
               message.what = commonData.USE_NOT_LOGIN_CODE;
               message.obj = commonData.USER_NOT_LOGIN;
               if(handler!=null)
               handler.sendMessage(message);
           }else if(status.equals("101")){
               Message message = new Message();
               message.what = commonData.SHOW_TIP_CODE;
               message.obj = commonData.SERVER_ERROR;
               if(handler!=null)
               handler.sendMessage(message);
           }
           else{
               Message message = new Message();
               message.what = commonData.GET_FRIEND_LIST_FAIL_CODE;
               message.obj = response.getString("msg");
               if(handler!=null)
               handler.sendMessage(message);
           }





        }catch (Exception e){
            CircleUtil.isAllChange = true;
            Log.e("好像有那里不对",e.toString());
            Message msg = new Message();
            msg.what = commonData.DATA_ERROR_CODE;
            msg.obj = commonData.DATA_ERROR;
            if(handler!=null)
            handler.sendMessage(msg);
            e.getStackTrace();
            LogToSd.error(e.getMessage());
            LogToSd.debug(response.toString());
        }
    }



    //获取好友列表失败的返回
    public  void GetFriendListFail(Context context,JSONObject errResponse,final Handler handler,final Object object){
        try{
            LogToSd.debug("打印获取好友列表失败的信息");
            Message msg = new Message();
            msg.what = commonData.CAN_NOT_CONNECT_SERVER_CODE;
            msg.obj = commonData.CAN_NOT_CONNECT_SERVER;
            if(handler!=null)
            handler.sendMessage(msg);
        }catch (Exception e){
            Message msg = new Message();
            msg.what = commonData.DATA_ERROR_CODE;
            msg.obj = commonData.DATA_ERROR;
            if(handler!=null)
            handler.sendMessage(msg);
            e.getStackTrace();
        }

    }

    //获取朋友圈详情成功
    public void  getCircleDetailSuccess(Context context,JSONObject response,final Handler handler, Object object){
        try{
            String status = response.getString("status");
            if(status.equals(commonData.DATA_NORMAL)){


                JSONObject res = response.getJSONObject("res");
                circleDetailPojo circlePojo = new circleDetailPojo();
//                circlePojo.setBelongStoreId(res.getString("u_belong_store_id"));
//                circlePojo.setBelongStoreName(res.getString("u_belong_store_name"));
                circlePojo.setUserCount(res.optString("sof_num"));
                circlePojo.setCircleMasterId(res.getString("u_id"));
                circlePojo.setCircleMasterNickName(res.getString("u_nickname"));
                circlePojo.setCircleMasterName(res.getString("u_name"));
                circlePojo.setCircleMasterPhone(res.getString("u_phone"));

                Message msg = new Message();
                msg.what = commonData.GET_CIRCLE_DETAIL_SUCCESS_CODE;
                msg.obj = circlePojo;
                if(handler!=null)
                handler.sendMessage(msg);
            }else if(status.equals("2")){
                Message msg = new Message();
                msg.what = commonData.SHOW_TIP_CODE;
                msg.obj = commonData.USER_DO_NOT_JOIN_CIRCLE;
                if(handler!=null)
                handler.sendMessage(msg);

            }else if(status.equals("100")){
                Message msg = new Message();
                msg.arg1 = commonData.GET_CIRCLE_DETAIL_WITHOUT_LOGIN;
                msg.what = commonData.USE_NOT_LOGIN_CODE;
                if(handler!=null)
                handler.sendMessage(msg);
            }
            else{

                LogToSd.debug("打印获取圈子详情失败的信息");
                LogToSd.debug(response.toString());
                Message msg = new Message();
                msg.what = commonData.GET_CIRCLE_DETAIL_FAIL_CODE;
                msg.obj = response.getString("msg");
                if(handler!=null)
                handler.sendMessage(msg);
            }

        }catch (Exception e){
            Message msg = new Message();
            msg.what = commonData.DATA_ERROR_CODE;
            msg.obj = commonData.DATA_ERROR;
            if(handler!=null)
            handler.sendMessage(msg);

            LogToSd.debug("获取朋友圈详服务器成功返回，但是前台解析失败失败:可能原因：json解析失败");
            LogToSd.debug(e.getMessage());
            LogToSd.debug(response.toString());
            e.getStackTrace();
        }
    }

    //获取朋友圈详情失败
    public void  getCircleDetailFail(Context context,JSONObject response,final Handler handler, Object object){
        try{
            LogToSd.debug("打印获取圈子详情失败的信息");
            Message msg = new Message();
            msg.what = commonData.CAN_NOT_CONNECT_SERVER_CODE;
            msg.obj = commonData.CAN_NOT_CONNECT_SERVER;
            if(handler!=null)
            handler.sendMessage(msg);
        }catch (Exception e){
            Message msg = new Message();
            msg.what = commonData.DATA_ERROR_CODE;
            msg.obj = commonData.DATA_ERROR;
            if(handler!=null)
            handler.sendMessage(msg);
            LogToSd.debug("获取朋友圈详服务器失败返回");
            LogToSd.debug(response.toString());
            e.getStackTrace();
        }
    }



}
